package com.zf.yichat.service.impl;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.dto.UserIndexDto;
import com.zf.yichat.mapper.SysSensitivesMapper;
import com.zf.yichat.model.SysSensitives;
import com.zf.yichat.model.SysUser;
import com.zf.yichat.service.SysSensitivesService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsPage;
import com.zf.yichat.utils.response.FsResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.Objects;

@Service
public class SysSensitivesServiceImpl implements SysSensitivesService {

    @Autowired
    private SysSensitivesMapper sensitivesMapper;

    @Override
    public FsResponse selectList(FsPage page, String word) {
        Example example = new Example(SysSensitives.class);
        if (StringUtils.isNotBlank(word)) {
            example.createCriteria().andLike("word", "%" + word + "%");
        }
        example.setOrderByClause("createdAt desc");

        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        return DtoChangeUtils.getPageList(sensitivesMapper.selectByExample(example), data -> {
            SysSensitives sensitives = new SysSensitives();
            BeanUtils.copyProperties(data, sensitives);
            return sensitives;
        });

    }

    @Override
    public int addOrEditSensitives(SysSensitives sensitives) {
        Integer id = sensitives.getId();
        if (Objects.nonNull(id)) {
            return sensitivesMapper.updateByPrimaryKey(sensitives);
        }else {
            sensitives.setCreatedAt(new Date());
            return sensitivesMapper.insert(sensitives);
        }
    }

    @Override
    public int updateSensitives(SysSensitives sensitives) {
        return sensitivesMapper.updateByPrimaryKey(sensitives);
    }

    @Override
    public int deleteSensitive(Integer id) {
        return sensitivesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SysSensitives selectSensitivesById(Integer id) {
        return sensitivesMapper.selectByPrimaryKey(id);
    }
}
